package com.hollycrm.ahunicom.sheet.business.logic;

import java.util.List;

import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
import net.sf.hibernate.HibernateException;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.hollycrm.ahunicom.sheet.business.CustomerAgentManager;
import com.hollycrm.ahunicom.sheet.domain.CustomerAgent;

public class HibernateCustomerAgentManager
    extends HibernateDaoSupport
    implements CustomerAgentManager {

  public List custLevels;

  public CustomerAgent getCustomerAgent(final String mobileNO) throws Exception {
    HibernateTemplate ht = getHibernateTemplate();
    return (CustomerAgent) ht.execute(
        new HibernateCallback() {

      public Object doInHibernate(Session session) throws
          HibernateException {
        Query query = session.createQuery("from cAgent in " +
                                                  CustomerAgent.class +
                                                  " where cAgent.mobileTeleNO='" +
                                                  mobileNO + "'");
        List ls = query.list();
        if(ls.size()>0)
          return (CustomerAgent)ls.get(0);
        else
          return new CustomerAgent();
      }
    });
  }

  public boolean checkLevel(String customerLevel) {
    boolean hasLevel = false;
    if(!custLevels.isEmpty()&&customerLevel!=null){
    for (int i = 0; i < custLevels.size(); i++) {
      if (customerLevel.equals(custLevels.get(i)))
        hasLevel = true;
    }
    }
    return hasLevel;
  }
  public List getCustLevels() {
    return custLevels;
  }
  public void setCustLevels(List custLevels) {
    this.custLevels = custLevels;
  }
}